package nuc.edu.controller;

import nuc.edu.dao.EmployeeDao;
import nuc.edu.dao.UserDao;
import nuc.edu.entities.Department;
import nuc.edu.entities.Employee;
import nuc.edu.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by 张朝亮
 */
@Controller
public class LoginController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private EmployeeDao employeeDao;


    @PostMapping(value = "/user/login")
    public String login(@RequestParam("lastname") String lastname,
                        @RequestParam("password") String password,
                         Map<String,Object> map, HttpSession session){
        User user = null;
        User usern = IsLogin(lastname,password);
        if((user = userDao.userLogin(lastname, password)) != null) {
            session.setAttribute("loginUser","欢迎管理员： "+lastname);
            session.setAttribute("type",1);
            session.setAttribute("userid",user.getId());
            return "redirect:/main.html";
        }else if(usern.getLastname()!=null){
            session.setAttribute("loginUser","欢迎用户： "+usern.getLastname());
            session.setAttribute("type",2);
            session.setAttribute("userid",usern.getId());
            return "redirect:/dep";
        }else{
            map.put("msg","用户名密码错误");
            return  "index";
        }

    }

    @GetMapping("/out")
    public String dengchu(){
        return "redirect:/index.html";
    }

    public User IsLogin(String lastname,String password){
        User user = new User();
        List<Employee> employees = (List<Employee>) employeeDao.getAll();
        for(Employee emp:employees){
            String usern = String.valueOf(emp.getId());
            String paw = emp.getPassword();
            if(lastname.equals(usern) && password.equals(paw)){
                user.setId(emp.getId());
                user.setLastname(emp.getLastname());
                user.setPassword(emp.getPassword());
                return user;
            }
        }
        return user;
    }

    /**
     * 修改管理员登录信息
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/upmag/{id}")
    public String upmag(@PathVariable("id") Integer id, Model model){
        User user = userDao.getUserByid(id);
        user.setType(1);
        model.addAttribute("upuser",user);
        return "emp/upuser";
    }

    /**
     * 修改普通用户登录信息
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/upus/{id}")
    public String upus(@PathVariable("id") Integer id, Model model){
        Employee employee = employeeDao.get(id);
        User user = new User();
        user.setId(employee.getId());
        user.setLastname(employee.getLastname());
        user.setPassword(employee.getPassword());
        user.setType(2);
        model.addAttribute("upuser",user);
        return "emp/upuser";

    }

    /**
     * 数据库修改管理员的用户名和密码
     * @param user
     * @return
     */
    @PostMapping("doupmag")
    public String doupmag(User user){
        userDao.updateus(user);
        return "index";
    }

    /**
     * 数据库修改职员的名字和密码
     * @param user
     * @return
     */
    @PostMapping("doupus")
    public String doupus(User user){
        employeeDao.updateemp(user);
        return "index";
    }

}
